<template>
  <div class="serach-result">
    <!-- 导航栏 -->
    <van-nav-bar
    title="sss搜索结果"
    left-arrow
    fixed
    @click-left="$router.back()">
    </van-nav-bar>
    <!-- 文章列表 -->
    <van-list
    v-model="loading"
    class="article-list"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad">
    <van-cell
    v-for="(item, index) in list"
    :key="index"
    :title="item.title"
    @click="$router.push('/article/' + item.art_id.toString())">
    </van-cell>
    </van-list>
  </div>
</template>
<script>
import { getSearchResult } from '../../api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false, // 加载
      finished: false,
      page: 1,
      refreshing: false
    }
  },
  // created () {
  //   var keyword = this.$route.query.keyword
  //   console.log(keyword)
  // },
  methods: {
    // 异步更新数据
    async onLoad () {
      try {
      // 异步更新数据
        const res = await getSearchResult(this.$route.query.keyword, this.page)
        console.log(res)
        const arr = res.data.data.results
        this.list.push(...arr)
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (this.list.length === 0) {
          this.finished = true
        }
        // 页码加一
        this.page++
      } catch (err) {
        console.log(err)
      }
    }
  }
}

</script>
<style lang="less" scoped>
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
